home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / spamexperts / dnslookup.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  3.7 KB  |  91 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import time
  5. import socket
  6. import subprocess
  7. gmail_ips = ('66.249.93.111', '66.249.93.109', '64.233.183.109', '72.14.205.109', '66.249.83.109', '66.249.83.111', '64.233.185.109', '64.233.185.111')
  8. at_home_ips = ('213.51.130.46', '213.51.146.46')
  9. telenet_ips = ('195.130.137.66', '195.130.132.42', '195.130.132.37', '195.130.137.65')
  10. yahoo_ips = ('66.163.171.139', '68.142.229.13')
  11. bellsouth_ips = ('205.152.59.16', '205.152.59.17')
  12. wanadoo_ips = ('193.252.22.231', '193.252.22.232')
  13. comcast_ips = ('204.127.198.10', '204.127.202.10', '63.240.76.10', '216.148.227.80')
  14. t_online_ips = ('194.25.134.47', '194.25.134.111')
  15.  
  16. class DNSLookup(object):
  17.     ONE_HOUR = 60 * 60
  18.     ONE_WEEK = ONE_HOUR * 24 * 7
  19.     
  20.     def __init__(self):
  21.         self.reset_cache()
  22.         self.generation_time = time.time() - self.ONE_HOUR
  23.  
  24.     
  25.     def reset_cache(self):
  26.         self.cache = { }
  27.         self.live_times = { }
  28.  
  29.     
  30.     def generate_cache(self):
  31.         p = subprocess.Popen('ipconfig /displaydns', stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.STDOUT, shell = True).stdout
  32.         for line in p.readlines():
  33.             line = line.strip()
  34.             if line and not line.strip('-'):
  35.                 host = previous
  36.             
  37.             if line.startswith('A (Host) Record'):
  38.                 self.cache[line.split(':')[1].strip()] = host
  39.             
  40.             if line.startswith('Time To Live'):
  41.                 self.live_times[host] = float(line.split(':')[1])
  42.             
  43.             previous = line
  44.         
  45.         for ip_list, cname in ((gmail_ips, 'pop.gmail.com'), (at_home_ips, 'mail.home.nl'), (telenet_ips, 'telenet-ops.be'), (yahoo_ips, 'mail.yahoo.com'), (bellsouth_ips, 'mail.bellsouth.net'), (comcast_ips, 'pop.comcast.net')):
  46.             for ip in ip_list:
  47.                 if ip not in self.cache:
  48.                     self.cache[ip] = cname
  49.                     continue
  50.             else:
  51.                 self.live_times[cname] = time.time() + self.ONE_WEEK
  52.         
  53.         self.generation_time = time.time()
  54.  
  55.     
  56.     def get_host(self, ip):
  57.         
  58.         try:
  59.             host = self.cache[ip]
  60.             if time.time() > self.live_times[host] + self.generation_time:
  61.                 host = None
  62.         except KeyError:
  63.             host = None
  64.  
  65.         if host:
  66.             return host
  67.         
  68.         if time.time() > self.generation_time + self.ONE_HOUR:
  69.             self.generate_cache()
  70.             return self.get_host(ip)
  71.         
  72.         old_timeout = socket.getdefaulttimeout()
  73.         socket.setdefaulttimeout(5)
  74.         
  75.         try:
  76.             host = socket.gethostbyaddr(ip)[0]
  77.         except socket.error:
  78.             host = ip
  79.  
  80.         socket.setdefaulttimeout(old_timeout)
  81.         self.cache[ip] = host
  82.         self.live_times[host] = time.time() + self.ONE_HOUR
  83.         return host
  84.  
  85.     
  86.     def __getitem__(self, ip):
  87.         return self.get_host(ip)
  88.  
  89.  
  90. lookup = DNSLookup()
  91.